import sys
import xml.etree.ElementTree as xml
from cStringIO import StringIO
from PySide2 import QtWidgets
from shiboken2 import wrapInstance
import pyside2uic
import maya.OpenMayaUI as mui


def loadUiType(uiFile):
    parsed = xml.parse(uiFile)
    widget_class = parsed.find('widget').get('class')
    form_class = parsed.find('class').text

    with open(uiFile, 'r') as f:
        o = StringIO()
        frame = {}

        pyside2uic.compileUi(f, o, indent=0)
        pyc = compile(o.getvalue(), '<string>', 'exec')
        exec pyc in frame

        # Fetch the base_class and form class based on their type in the xml from designer
        form_class = frame['Ui_%s' % form_class]
        base_class = getattr(QtWidgets, widget_class)
    return form_class, base_class

def getMayaWindow():
    ''' pointer to the maya main window '''
    return wrapInstance(long(mui.MQtUtil.mainWindow()), QtWidgets.QWidget)
